import { ElMessage } from 'element-plus'

export function isImageFile(fileName) {
  let fileArr = fileName.split('.')
  if (fileArr.length < 2) return false
  let suffix = fileArr[fileArr.length - 1]
  let imgList = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp']
  let result = imgList.some((item) => item === suffix)
  return result
}

export const getValType = (val) => {
  return Object.prototype.toString.call(val).replace('[object ', '').replace(']', '')
}

//随机生成id
export function generateId() {
  let color = '',
    letters = '0123456789ABCDEF'
  for (let i = 0; i < 5; i++) {
    color += letters[Math.floor(Math.random() * 16)]
  }
  return new Date().getTime() + color
}

//显示消息
export const showToast = async (message, type: string = 'success') => {
  return new Promise((resolve, reject) => {
    ElMessage({ message: message, type: type })
    setTimeout(() => resolve(true), 1000)
  })
}

//获取下一个数字转字符串
export const getNextNumberStr = (numStr) => {
  let totalLen = 2,
    num = parseInt(numStr) + 1
  let leftNumList = Array.from({ length: totalLen - num.toString().length }, (_, i) => 0)
  return `${leftNumList.join('')}${num}`
}

//获取下一个字节点的id
export const getNextChildNumberStr = (numStr) => {
  let preNumber = numStr.substring(0, numStr.lastIndexOf('_'))
  let lastNumber = numStr.substring(numStr.lastIndexOf('_') + 1, numStr.length)
  return preNumber + '_' + getNextNumberStr(lastNumber)
}
